#include "StdAfx.h"
#include "imageview.h"
#include "imageview_p.h"

ImageView::ImageView(QWidget *parent)
	: QGraphicsView(parent)
	, d_ptr(new ImageViewPrivate(this))
{

}

ImageView::ImageView(ImageViewPrivate &dd, QWidget *parent)
	: QGraphicsView(parent)
	, d_ptr(&dd)
{

}

ImageView::~ImageView()
{

}

QImage 
ImageView::image() const
{
	return d_func()->image;
}

void 
ImageView::setImage(const QImage& image)
{
	Q_D(ImageView);
	d->image = image;
	viewport()->update();
}

bool 
ImageView::isScaleVisible() const
{
	return d_func()->scaleVisible;
}

void 
ImageView::setScaleVisible(const bool scaleVisible)
{
	Q_D(ImageView);
	d->scaleVisible = scaleVisible;
	viewport()->update();
}

QSize 
ImageView::scaleSize() const
{
	return d_func()->scaleSize;
}

void 
ImageView::setScaleSize(const QSize& scaleSize)
{
	Q_D(ImageView);
	d->scaleSize = scaleSize;
}

QColor 
ImageView::scaleColor() const
{
	return d_func()->scaleColor;
}

void 
ImageView::setScaleColor(const QColor& scaleColor)
{
	Q_D(ImageView);
	d->scaleColor = scaleColor;
}

uint 
ImageView::scaleLength() const
{
	return d_func()->scaleLength;
}

void 
ImageView::setScaleLength(const uint scaleLength)
{
	Q_D(ImageView);
	d->scaleLength = scaleLength;
}

void 
ImageView::paintEvent(QPaintEvent* event)
{
	Q_D(ImageView);
	if (!image().isNull())
	{
		paintImage();
	}
	if(isScaleVisible())
	{
		paintScale();
	}
	QGraphicsView::paintEvent(event);
}

void 
ImageView::paintImage()
{
	Q_D(ImageView);
	QPainter painter(viewport());
	QScrollBar * horScrollBar =	horizontalScrollBar();
	QScrollBar *verScrollBar = verticalScrollBar();	
	QRect rect = viewport()->rect();
	painter.drawImage(rect.topLeft(), 
					d->image, 
					QRect(horScrollBar->value(),
					verScrollBar->value(),
					d->image.width(),
					d->image.height()));
}

void 
ImageView::paintScale()
{
	Q_D(ImageView);
	QPainter painter(viewport());
	QRect rect = viewport()->rect();
	painter.save();

	painter.setPen(scaleColor());
	// viewport property
	int width = rect.width();
	int height = rect.height();

	int halfWidth = width / 2;
	int halfHeight = height / 2;

	int scaleWidth = d->scaleSize.width();
	int scaleHeight = d->scaleSize.height();
	// draw veritcal scale
	painter.drawLine(QPoint(0,halfHeight),QPoint(width,halfHeight));
	QPoint tl,bl,tr,br;	

	for(int i = 1; i <= halfWidth / scaleWidth; ++i)
	{
		int lx = halfWidth - scaleWidth * i;	
		int rx = halfWidth + scaleWidth * i;		
		if(i % 2 == 0)
		{
			tl = QPoint(lx, halfHeight - scaleLength() * 2);
			bl = QPoint(lx, halfHeight + scaleLength() * 2);
			tr = QPoint(rx, halfHeight - scaleLength() * 2);
			br = QPoint(rx, halfHeight + scaleLength() * 2);	
		}
		else
		{
			tl = QPoint(lx, halfHeight - scaleLength());
			bl = QPoint(lx, halfHeight + scaleLength());
			tr = QPoint(rx, halfHeight - scaleLength());
			br = QPoint(rx, halfHeight + scaleLength());	
		}
		painter.drawLine(tl,bl);
		painter.drawLine(tr,br);		
	}

	// draw horizontal scale
	painter.drawLine(QPoint(halfWidth,0),QPoint(halfWidth,height));
	
	for(int i = 1; i <= halfHeight/scaleHeight ; ++i )
	{
		int ty = halfHeight - scaleHeight * i;	
		int by = halfHeight + scaleHeight * i;	

		if(i % 2 == 0)
		{
			tl = QPoint(halfWidth - scaleLength() * 2, ty);
			bl = QPoint(halfWidth - scaleLength() * 2, by);
			tr = QPoint(halfWidth + scaleLength() * 2, ty);
			br = QPoint(halfWidth + scaleLength() * 2, by);	
		}
		else
		{
			tl = QPoint(halfWidth - scaleLength(), ty);
			bl = QPoint(halfWidth - scaleLength(), by);
			tr = QPoint(halfWidth + scaleLength(), ty);
			br = QPoint(halfWidth + scaleLength(), by);	
		}
		painter.drawLine(tl,tr);
		painter.drawLine(bl,br);
	}
	painter.restore();
}

